package org.daoImpl;

import org.dao.UserDao;
import org.model.User;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by tedzy on 2015/5/11.
 * UserDao实现
 */
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
    @Override
    public User checkLogin(String username, String password) {
        User user = null;
        List list = this.getHibernateTemplate().find("from User where username=? and password=?", username, password);
        if(list.size() == 1){
            user = (User) list.get(0);
        }
        return user;
    }

    @Override
    public String getUsername(int uid) {
        return null;
    }

    @Override
    public User getUser(int uid) {
        User user = null;
        List list = this.getHibernateTemplate().find("from User where id=?", uid);
        if(list.size() > 0){
            user = (User) list.get(0);
        }
        return user;
    }

    @Override
    public boolean changePassword(int uid, String newPassword) {
        return false;
    }

    @Override
    public boolean addUser(User user) {
        this.getHibernateTemplate().save(user);
        return true;
    }

    @Override
    public int countUsername(String username) {
        List list = this.getHibernateTemplate().find("from User where username=?", username);
        return list.size();
    }

    @Override
    public void update(User user) {
        this.getHibernateTemplate().update(user);
    }
}
